﻿using AppServiceDtoToEntityDemo.Application.Orders.Dtos;
using AppServiceDtoToEntityDemo.Domain.Orders;
using static AppServiceDtoToEntityDemo.Domain.Orders.Order;
using static AppServiceDtoToEntityDemo.Domain.Orders.OrderItem;

namespace AppServiceDtoToEntityDemo.Application.Orders.Assemblers
{
    public interface ICreateOrderDtoAssembler
    {
        Order ComposeOrder(CreateOrderDto input);
    }

    public class CreateOrderDtoAssembler : ICreateOrderDtoAssembler
    {
        private readonly IOrderRepository _orderRepository;

        public CreateOrderDtoAssembler(IOrderRepository orderRepository)
        {
            _orderRepository = orderRepository;
        }

        public Order ComposeOrder(CreateOrderDto input)
        {
            var maxNum = _orderRepository.GetMaxNum();

            var order = OrderBuilder.CreateBuilder()
                .SetName(input.Name)
                .SetNum(maxNum)
                .CategoryFrom(input.CategoryId)
                .SetDescription1(input.Description1)
                .SetDescription2(input.Description2)
                .SetDescription3(input.Description3)
                .SetDescription4(input.Description4)
                .SetDescription5(input.Description5)
                .SetDescription6(input.Description6)
                .SetDescription7(input.Description7)
                .SetDescription8(input.Description8)
                .SetDescription9(input.Description9)
                .SetDescription10(input.Description10)
                .SetDescription11(input.Description11)
                .SetDescription12(input.Description12)
                .SetDescription13(input.Description13)
                .SetDescription14(input.Description14)
                .SetDescription15(input.Description15)
                .Build();

            foreach (var createOrderItem in input.CreateOrderItems)
            {
                var orderItem = OrderItemBuilder.CreateBuilder()
                    .ProductFrom(createOrderItem.ProductId)
                    .SetAmount(createOrderItem.Amount)
                    .SetDescription1(createOrderItem.Description1)
                    .SetDescription2(createOrderItem.Description2)
                    .SetDescription3(createOrderItem.Description3)
                    .SetDescription4(createOrderItem.Description4)
                    .SetDescription5(createOrderItem.Description5)
                    .SetDescription6(createOrderItem.Description6)
                    .SetDescription7(createOrderItem.Description7)
                    .SetDescription8(createOrderItem.Description8)
                    .SetDescription9(createOrderItem.Description9)
                    .SetDescription10(createOrderItem.Description10)
                    .SetDescription11(createOrderItem.Description11)
                    .SetDescription12(createOrderItem.Description11)
                    .SetDescription13(createOrderItem.Description11)
                    .SetDescription14(createOrderItem.Description11)
                    .SetDescription11(createOrderItem.Description11)
                    .Build();

                order.AddOrderItem(orderItem);
            }

            return order;
        }
    }
}
